package com.jrh.biz.category;

import java.util.ArrayList;
import java.util.List;

import com.jrh.dal.CategoryDao;
import com.jrh.domain.entity.Category;

public class CategoryServiceImpl implements CategoryService {

    private CategoryDao categoryDao;

    public void setCategoryDao(CategoryDao categoryDao) {
        this.categoryDao = categoryDao;
    }

    @Override
    public List<Category> listCategoryByParent() {
        List<Category> list = categoryDao.listAllCategory();
        List<Category> resultList = new ArrayList<Category>();
        long parentId = -1;
        for (Category category : list) {
            if (category == null) {
                continue;
            }
            if (category.getIsParent()!=null && category.getIsParent() == 1) {
                resultList.add(category);
                parentId = category.getId();
            } else if (category.getParent() == parentId) {
                List<Category> childCategorys = resultList.get(resultList.size() - 1).getChildCategoryList();
                if (childCategorys == null) {
                    childCategorys = new ArrayList<Category>();
                }
                childCategorys.add(category);
                resultList.get(resultList.size() - 1).setChildCategoryList(childCategorys);
            }
        }
        return resultList;
    }
}
